package com.sgb.kjwl.viewmodel.self;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.CarriageSelectEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.EditOilActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditeOilModel extends BaseViewModel {
    EditOilActivity activity;
    public SelfRepertory repertory;

    public EditeOilModel(EditOilActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void oilType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().oilType().enqueue(new Callback<BaseEntity<List<CarriageSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarriageSelectEntity>>> call, Response<BaseEntity<List<CarriageSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.setTypePop(response.body().getData());
                } else {
                    MToast.showToast(activity.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarriageSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void oilBrand() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getBrand().enqueue(new Callback<BaseEntity<List<CarriageSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarriageSelectEntity>>> call, Response<BaseEntity<List<CarriageSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.setBrandPop(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarriageSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void saveOil(int id,String oilTypeNo,String brandNo,double price,double standardPrice){
        Map map=new HashMap();
        if(id!=0){
            map.put("id",id);
        }
        map.put("oilTypeNo",oilTypeNo);
        map.put("brandNo",brandNo);
        map.put("price",price);
        map.put("standardPrice",standardPrice);

        DialogHelper.showProgressDialog(activity, null, "提交中。。。", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().saveOil(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    MToast.showToast(activity, "提交成功");
                    activity.finish();
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }
}
